package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.FeedBackType;
import com.lc.luzhengtang.conn.ConnFeedBack;
import com.lc.luzhengtang.conn.ConnFeedBackType;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.lc.luzhengtang.R.id.text_logistics;

/**
 * Created by Administrator on 2017/5/5.
 */
public class FeedBackActivity extends BaseActivity {


    @BindView(R.id.rv_searchhistory)
    RecyclerView rvSearchhistory;
    List<String> mDatas;
    //    FeedBackAdapter searchHistoryAdapter;

    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_Software)
    TextView textSoftware;
    @BindView(text_logistics)
    TextView textLogistics;
    @BindView(R.id.text_commodity)
    TextView textCommodity;
    @BindView(R.id.text_other)
    TextView textOther;
    /*    @BindView(R.id.id_flowlayout)
        TagFlowLayout idFlowlayout;*/
    @BindView(R.id.text_submit)
    TextView textSubmit;
    @BindView(R.id.editText_phone)
    EditText editTextPhone;
    @BindView(R.id.editText_content)
    EditText editTextContent;
    private String[] mVals = new String[]
            {"Hello", "Android", "Weclome Hi ", "Button"};

    //意见反馈类型
    private String type = null;

    FeedBackType feedBackTypes;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        mDatas = new ArrayList<>();
        feedBackTypes = new FeedBackType();
        for (int i = 0; i < 4; i++) {
            mDatas.add("干他妈的" + i);
        }
        rvSearchhistory.setLayoutManager(new FullyGridLayoutManager(FeedBackActivity.this, 3));

/*

        final LayoutInflater mInflater = LayoutInflater.from(this);
        idFlowlayout.setAdapter(new TagAdapter<String>(mVals) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        idFlowlayout, false);
                tv.setText(s);
                return tv;
            }
        });
*/

        getFeedBack();
    }

    @OnClick({R.id.img_news, R.id.text_Software, text_logistics, R.id.text_commodity, R.id.text_other, R.id.text_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_Software:
                textSoftware.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback_backgroune));
                textLogistics.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textCommodity.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textOther.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textSoftware.setTextColor(getResources().getColor(R.color.white));
                textLogistics.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textCommodity.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textOther.setTextColor(getResources().getColor(R.color.colorTextGrey));
                type = "1";
                break;
            case text_logistics:
                textLogistics.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback_backgroune));
                textSoftware.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textCommodity.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textOther.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textLogistics.setTextColor(getResources().getColor(R.color.white));
                textSoftware.setTextColor(getResources().getColor(R.color.colorTextGrey));

                textCommodity.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textOther.setTextColor(getResources().getColor(R.color.colorTextGrey));
                type = "2";

                break;
            case R.id.text_commodity:
                textCommodity.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback_backgroune));
                textSoftware.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textLogistics.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textOther.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textCommodity.setTextColor(getResources().getColor(R.color.white));
                textLogistics.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textSoftware.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textOther.setTextColor(getResources().getColor(R.color.colorTextGrey));

                type = "3";
                break;
            case R.id.text_other:
                textOther.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback_backgroune));
                textCommodity.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textSoftware.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textLogistics.setBackground(getResources().getDrawable(R.drawable.shape_juxing_feedback));
                textOther.setTextColor(getResources().getColor(R.color.white));
                textCommodity.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textLogistics.setTextColor(getResources().getColor(R.color.colorTextGrey));
                textSoftware.setTextColor(getResources().getColor(R.color.colorTextGrey));
                type = "4";
                break;

            case R.id.text_submit:
                if (TextUtils.isEmpty(type)) {
                    LovelyToast.makeText(FeedBackActivity.this, "请选择问题类型", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (TextUtils.isEmpty(editTextContent.getText().toString().trim())) {
                    LovelyToast.makeText(FeedBackActivity.this, "反馈内容不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!TextUtils.isEmpty(editTextPhone.getText().toString().trim()) && !UtilMatches.checkMobile(editTextPhone.getText().toString())) {
                    LovelyToast.makeText(FeedBackActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }

              /*  else if (TextUtils.isEmpty(editTextPhone.getText().toString().trim())) {
                    LovelyToast.makeText(FeedBackActivity.this, "手机号不能为空", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (editTextPhone.getText().toString().trim().length() < 6 || editTextPhone.getText().toString().trim().length() > 16) {
                    LovelyToast.makeText(FeedBackActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!UtilMatches.checkMobile(editTextPhone.getText().toString())) {
                    LovelyToast.makeText(FeedBackActivity.this, "请输入正确的手机号", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                }*/

                else {
                    sendFeedBackData();
                }


                break;
        }
    }

    public void sendFeedBackData() {
        new ConnFeedBack(BaseApplication.basePreferences.readUid(), editTextPhone.getText().toString().trim(), type, editTextContent.getText().toString(), new AsyCallBack<CommenBean>() {
            @Override
            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                super.onSuccess(toast, type, o, commenBean);
                LovelyToast.makeText(FeedBackActivity.this, commenBean.getMessage(), 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
                finish();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(FeedBackActivity.this, "网络连接异常", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(FeedBackActivity.this, false);
    }

/*    private void initAdapter() {
        searchHistoryAdapter = new FeedBackAdapter(mDatas);

        searchHistoryAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//        pullToRefreshAdapter.setAutoLoadMoreSize(3);
        Drawable drawable = ContextCompat.getDrawable(FeedBackActivity.this, R.drawable.line_divider);
        rvSearchhistory.addItemDecoration(new SimpleDividerItemDecoration(FeedBackActivity.this, drawable, 25));
        rvSearchhistory.setAdapter(searchHistoryAdapter);

        rvSearchhistory.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Toast.makeText(FeedBackActivity.this, Integer.toString(position), Toast.LENGTH_LONG).show();


            }
        });

        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent)
            {
                Toast.makeText(FeedBackActivity.this, mVals[position], Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }*/


    public void getFeedBack() {

        new ConnFeedBackType(new AsyCallBack<FeedBackType>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, FeedBackType feedBackType) throws Exception {
                super.onSuccess(toast, type, o, feedBackType);
                feedBackTypes = feedBackType;
                textSoftware.setText(feedBackType.getData().get(0));
                textLogistics.setText(feedBackType.getData().get(1));
                textCommodity.setText(feedBackType.getData().get(2));
                textOther.setText(feedBackType.getData().get(3));
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }

        ).execute(FeedBackActivity.this, false);

    }


}
